import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

const String themeMode = 'theme';
const String colorMode = 'color';

/// theme mode
enum AppThemeMode { light, dark }

/// color mode，特定view背景颜色
enum AppThemeColorMode { indigo, orange, pink, teal, blue, cyan, purple }

/// font mode
enum AppFontMode {
  ///默认字体
  roboto,

  ///第三方字体
  maShanZheng
}

AppThemeMode getInitThemeMode(String mode) {
  if (kDebugMode) {
    print(mode);
  }
  AppThemeMode appThemeMode = AppThemeMode.values.firstWhere(
      (element) => element.toString() == mode,
      orElse: () => AppThemeMode.light);
  return appThemeMode;
}

AppThemeColorMode getInitColorMode(String mode) {
  AppThemeColorMode colorMode = AppThemeColorMode.values.firstWhere(
      (element) => element.toString() == mode,
      orElse: () => AppThemeColorMode.blue);
  return colorMode;
}

class GradientColor {
  final AppThemeColorMode mode;
  final Color start;
  final Color end;

  GradientColor(this.mode, this.start, this.end);
}

class AppTheme {
  static Color modeMainColor(AppThemeMode mode) {
    if (mode == AppThemeMode.dark) {
      return Colors.black;
    }
    return Colors.white;
  }

  static Color themeMainColor(AppThemeColorMode mode) {
    return getGradientColor(mode).end;
  }

  static Color themeSecondColor(AppThemeColorMode mode) {
    return getGradientColor(mode).start;
  }

  ///渐变色
  static List<GradientColor> gradientColors = [
    //Colors.indigo
    GradientColor(
        AppThemeColorMode.indigo,
        const Color.fromARGB(255, 101, 89, 184),
        const Color.fromARGB(255, 112, 113, 228)),
    //Colors.orange
    GradientColor(
        AppThemeColorMode.orange,
        const Color.fromARGB(255, 253, 145, 141),
        const Color.fromARGB(255, 252, 178, 146)),
    //Colors.pink
    GradientColor(
        AppThemeColorMode.pink,
        const Color.fromARGB(255, 242, 79, 136),
        const Color.fromARGB(255, 249, 88, 110)),
    //Colors.teal
    GradientColor(
        AppThemeColorMode.teal,
        const Color.fromARGB(255, 56, 155, 148),
        const Color.fromARGB(255, 80, 201, 145)),
    //Colors.blue
    GradientColor(AppThemeColorMode.blue, const Color(0xFF738AE6),
        const Color(0xFF5C5EDD)),
    //Colors.cyan
    GradientColor(
        AppThemeColorMode.cyan,
        const Color.fromARGB(255, 21, 177, 202),
        const Color.fromARGB(255, 25, 209, 201))
  ];

  static GradientColor getGradientColor(AppThemeColorMode mode) {
    for (var value in gradientColors) {
      if (mode == value.mode) {
        return value;
      }
    }
    return gradientColors.first;
  }

  AppTheme._(this.currentThemeMode, this.currentFontMode, this.currentColorMode,
      this.fontFamily, this.gradientColor);

  // static final AppTheme appTheme = AppTheme._(currentThemeMode, currentFontMode, currentColorMode, fontFamily);

  AppThemeMode currentThemeMode;
  AppThemeColorMode currentColorMode;
  AppFontMode currentFontMode;

  String fontFamily;
  String numFontFamily = 'Montserrat';
  GradientColor gradientColor;

  void setThemeState(AppThemeMode themeMode, AppThemeColorMode themeColorMode,
      AppFontMode fontMode) {
    currentThemeMode = themeMode;
    currentColorMode = themeColorMode;
    currentFontMode = fontMode;
    fontFamily = getFontFamily(currentFontMode);
    gradientColor = getGradientColor(currentColorMode);
  }

  ThemeData themeData() {
    if (currentThemeMode == AppThemeMode.dark) {
      return darkTheme();
    } else {
      return lightTheme();
    }
  }

  bool isDark() {
    return currentThemeMode == AppThemeMode.dark;
  }

  ThemeData lightTheme() {
    return ThemeData.light().copyWith(
        primaryColor: const Color(0xFFF2F7FB),
        primaryColorDark: const Color(0xFF6B6B6B),
        colorScheme:
            ColorScheme.fromSwatch().copyWith(secondary: gradientColorEnd()));
  }

  ThemeData darkTheme() {
    return ThemeData.dark().copyWith(
        primaryColor: const Color(0xFF17262A),
        primaryColorDark: const Color(0xFF6B6B6B),
        colorScheme:
            ColorScheme.fromSwatch().copyWith(secondary: gradientColorEnd()));
  }

  String getFontFamily(AppFontMode mode) {
    switch (mode) {
      case AppFontMode.maShanZheng:
        return 'MaShanZheng';
      case AppFontMode.roboto:
        // TODO: Handle this case.
        break;
    }
    return 'Roboto';
  }

  ///----------字体----------
  /// 黑/白
  TextStyle headline1(
      {required FontWeight fontWeight,
      required double fontSize,
      required Color textColor}) {
    return TextStyle(
        fontWeight: fontWeight,
        fontSize: fontSize,
        color: textColor,
        fontFamily: fontFamily);
  }

  /// Edit hint text
  TextStyle hint({required FontWeight fontWeight, required double fontSize}) {
    return TextStyle(
        fontWeight: fontWeight,
        fontSize: fontSize,
        color: isDark()
            ? Colors.white.withOpacity(0.5)
            : Colors.black.withOpacity(0.5),
        fontFamily: fontFamily);
  }

  /// theme color text
  TextStyle themeText(
      {required FontWeight fontWeight,
      required double fontSize,
      required Color textColor}) {
    return TextStyle(
        fontWeight: fontWeight,
        fontSize: fontSize,
        color: textColor,
        fontFamily: fontFamily);
  }

  /// 数字
  TextStyle numHeadline1(
      {required FontWeight fontWeight,
      required double fontSize,
      required Color textColor}) {
    return TextStyle(
        fontWeight: fontWeight,
        fontSize: fontSize,
        color: textColor,
        fontFamily: numFontFamily);
  }

  ///数字
  TextStyle numHeadline2(
      {required FontWeight fontWeight,
      required double fontSize,
      required Color textColor}) {
    return TextStyle(
        fontWeight: fontWeight,
        fontSize: fontSize,
        color: textColor,
        fontFamily: numFontFamily);
  }

  TextStyle textStyle(
      {required Color textColor,
      required FontWeight fontWeight,
      required double fontSize}) {
    return TextStyle(
        fontFamily: getFontFamily(currentFontMode),
        fontSize: fontSize,
        fontWeight: fontWeight,
        color: textColor);
  }

  ///---------背景----------
  ///容器背景颜色
  Color containerBackgroundColor() {
    return isDark() ? const Color(0xFF233355) : const Color(0xFFF2F7FB);
  }

  ///所有卡片背景颜色
  Color cardBackgroundColor() {
    return isDark() ? const Color(0xFF294261) : Colors.white;
  }

  Color normalColor() {
    return isDark() ? Colors.white : Colors.black;
  }

  ///渐变开始颜色
  Color gradientColorStart() {
    return gradientColor.start;
  }

  ///渐变结束颜色
  Color gradientColorEnd() {
    return gradientColor.end;
  }

  ///背景统一渐变色
  LinearGradient containerGradient(
      {required Alignment begin, required Alignment end}) {
    return LinearGradient(
        colors: [gradientColorStart(), gradientColorEnd()],
        begin: begin,
        end: end);
  }

  ///背景统一渐变色
  LinearGradient containerGradientWithOpacity(
      {required Alignment begin,
      required Alignment end,
      required double opacity}) {
    return LinearGradient(colors: [
      gradientColorStart().withOpacity(opacity),
      gradientColorEnd().withOpacity(opacity)
    ], begin: begin, end: end);
  }

  ///通一阴影
  List<BoxShadow> containerBoxShadow() {
    return [
      BoxShadow(
          color: Colors.black.withOpacity(0.1),
          offset: const Offset(5, 5),
          blurRadius: 16)
    ];
  }

  ///带颜色阴影
  List<BoxShadow> coloredBoxShadow() {
    return [
      BoxShadow(
          color: gradientColorStart().withOpacity(0.3),
          offset: const Offset(5, 5),
          blurRadius: 16)
    ];
  }

  Color selectColor() {
    return isDark() ? Colors.white : Colors.black;
  }

  static const Color iconColor = Colors.grey;
}
